package com.example.administrator.personalmail.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.model.Category;

import java.util.List;


public class CategoryFirstAdapter extends RecyclerView.Adapter {
    private List<Category> mDatas;

    public CategoryFirstAdapter(List<Category> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.template_single_text,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder)holder;
        viewHolder.textView.setText(mDatas.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_category_first);

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mCategoryFirstListenner!=null){
                        mCategoryFirstListenner.onItemClick(mDatas.get(getLayoutPosition()).getId());
                    }
                }
            });
        }
    }

    private CategoryFirstListenner mCategoryFirstListenner;

    public void setCategoryFirstListenner(CategoryFirstListenner categoryFirstListenner){
        mCategoryFirstListenner = categoryFirstListenner;
    }

    public interface CategoryFirstListenner{
        void onItemClick(long categoryId);
    }
}
